<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Task;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class TaskController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Task(), function (Grid $grid) {
            $grid->model()->orderBy('updated_at', 'desc');
            $grid->column('id')->sortable();
            $grid->column('name');
            $grid->column('type')->display(function ($type) {
                return \App\Models\Task::TYPE[$type];
            });
            $grid->column('content');
            $grid->column('number');
            $grid->column('award_number');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Task(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('type')->using(\App\Models\Task::TYPE);
            if ($show->model()->type == \App\Models\Task::TYPE_SJ) {
                $show->field('platform', '对接平台')->using(\App\Models\Task::PLATFORM);
                $show->field('platform_id', '对接平台活动ID');
            }
            if ($show->model()->type == \App\Models\Task::TYPE_XS) {
                $show->field('action', '任务动作')->using(\App\Models\Task::ACTION);
            }
            $show->field('content');
            $show->field('number');
            $show->field('award_number');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Task(), function (Form $form) {
            $form->display('id');
            $form->text('name');
            $form->radio('type')
                ->options(\App\Models\Task::TYPE)
                ->default(\App\Models\Task::TYPE_XS)
                ->when(\App\Models\Task::TYPE_SJ, function (Form $form) {
                    $form->radio('platform', '对接平台')->options(\App\Models\Task::PLATFORM)->default(\App\Models\Task::PLATFORM_DIDI)
                        ->when(\App\Models\Task::PLATFORM_DIDI, function (Form $form) {
                            $form->html('<a href="https://union.didi.cn/#/promoteOverview" target="_blank">点击跳转获取ID</a>');
                        })->when(\App\Models\Task::PLATFORM_JTK, function (Form $form) {
                            $form->html('<a href="https://www.jutuike.com/single_page" target="_blank">点击跳转获取ID</a>');
                        });
                    $form->text('platform_id', '对接平台活动ID')->default(0);
                })
                ->when(\App\Models\Task::TYPE_XS, function (Form $form) {
                    $form->radio('action', '任务动作')->options(\App\Models\Task::ACTION)->default(\App\Models\Task::ACTION_WX);
                });
            $form->text('content');
            $form->text('number');

            $form->text('award_number')
                ->default(0)
                ->saving(function ($v) {
                    return $v ?: 0;
                });

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
